package com.zby.business.util;

import android.Manifest;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.v4.app.ActivityCompat;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.Toast;

import com.zby.business.R;

/**
 * Created by BY on 2015/12/28.
 */
public class Util {
    public static final String BASE_URL = "http://10.10.186.151:8080/ssh/";
    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static final String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };
    private static LayoutInflater inflater;

    public static void toast(Context context, String text) {
        Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
    }

    public static void toast(Context context, View view) {
        Toast toast = new Toast(context);
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.setView(view);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
    }

    public static void alert(Context context, View view) {
        AlertDialog alert = new AlertDialog.Builder(context, AlertDialog.THEME_HOLO_LIGHT).create();
        alert.setView(view);
        alert.show();
    }

    public static void alertMessage(Context context, View view) {
        AlertDialog alert = new AlertDialog.Builder(context, AlertDialog.THEME_HOLO_LIGHT).create();
        alert.setView(view);
        alert.show();
    }

    public static void loadingAlert(Context context) {
        inflater = LayoutInflater.from(context);
        AlertDialog dialog = new AlertDialog.Builder(context).create();
        View view = inflater.inflate(R.layout.loading_image, null);
        dialog.setView(view);
        dialog.show();
        WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        Display display = manager.getDefaultDisplay();
        Window window = dialog.getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = (int) (display.getWidth() * 0.47);
        window.setAttributes(lp);
    }


    public static void verifyStoragePermissions(Activity activity) {
        int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);

        if (permission != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(
                    activity,
                    PERMISSIONS_STORAGE,
                    REQUEST_EXTERNAL_STORAGE
            );
        }
    }
}
